import { observable, action, autorun, toJS } from 'mobx';
import { WeaLocaleProvider } from 'ecCom';
import {WeaForm, WeaTableNew} from 'comsMobx'
import {message, Button } from "antd";
import * as Apis from '../../apis/info';
import { ListStore } from '../listStore';
import {Text} from '../../components/util/text';
const text = new Text();
const {TableStore} = WeaTableNew;
const getLabel = WeaLocaleProvider.getLabel;
export default class infoCountStore extends ListStore {

    @observable responseData = {};
    @observable title = "";
    @observable status = {
        loading: false,
        isShowDetail : ""      //是否包含得分明细
    }
    @observable form = new WeaForm();
    @observable condition = [];
    @observable previewFlag = false;    //判断当前是否预览状态
    @observable isRight = false;        //判断是否有权限
    @observable loadingSpin = true;     //全局loading

    type = ""; //当前选择的方式
    @observable htmlJson;
    //     htmlJson = {
    // 	title: '政务信息积分表',
    // 	dateRange: '2018-11-01至2018-11-30',
    // 	list: [{
    // 			reportOrgName: '绍兴市委',
    // 			sumScore: 90,
    // 			count: 3,
    // 			infoSiftList: [{
    // 				infoSiftTitle: '测试发文20181210',
    // 				column: '互联网+',
    // 				score: 20,
    // 				jounalTitle: '《每日政务》第5期'
    // 			}, {
    // 				infoSiftTitle: '测试信息采编',
    // 				column: '多贵合一',
    // 				score: 50,
    // 				jounalTitle: '《每日政务》第5期'
    // 			}, {
    // 				infoSiftTitle: 'HTMLJSON。。。。。。',
    // 				column: '盛天地太',
    // 				score: 20,
    // 				jounalTitle: '《每日政务》第5期'
    // 			},]
    // 		},{
    // 			reportOrgName: '绍兴发改委',
    // 			sumScore: 60,
    // 			count: 3,
    // 			infoSiftList: [{
    // 				infoSiftTitle: '信息',
    // 				column: '栏目一',
    // 				score: 20,
    // 				jounalTitle: '《每日政务》第5期'
    // 			}, {
    // 				infoSiftTitle: '信息',
    // 				column: '栏目一',
    // 				score: 20,
    // 				jounalTitle: '《每日政务》第5期'
    // 			}, {
    // 				infoSiftTitle: '信息',
    // 				column: '栏目一',
    // 				score: 20,
    // 				jounalTitle: '《每日政务》第5期'
    // 			},]
    // 		},{
    // 			reportOrgName: '绍兴大哒哒哒哒哒',
    // 			sumScore: 60,
    // 			count: 3,
    // 			infoSiftList: [{
    // 				infoSiftTitle: '信息',
    // 				column: '栏目一',
    // 				score: 20,
    // 				jounalTitle: '《每日政务》第5期'
    // 			}, {
    // 				infoSiftTitle: '信息',
    // 				column: '栏目一',
    // 				score: 20,
    // 				jounalTitle: '《每日政务》第5期'
    // 			}, {
    // 				infoSiftTitle: '信息',
    // 				column: '栏目一',
    // 				score: 20,
    // 				jounalTitle: '《每日政务》第5期'
    // 			},]
    // 		},

    // 	]
    // };      //预览的html文档的json

    //     htmlJson = {
    // 	title: '政务信息积分表',
    // 	dateRange: '2018-11-01至2018-11-30',
    // 	list: [{
    // 		reportOrgName: '绍兴市委',
    // 		sumScore: 60,
    // 		count: 3,
    // 		infoAdjustList: [{
    // 			adjustReason: '搞的不错',
    // 			adjustType: '0', //（0奖励、1扣分）
    // 			adjustScore: 20,
    // 		}, {
    // 			adjustReason: '不认真',
    // 			adjustType: '1', //（0奖励、1扣分）
    // 			adjustScore: 50,
    // 		}]
    // 	},{
    // 		reportOrgName: '绍兴市委',
    // 		sumScore: 60,
    // 		count: 3,
    // 		infoAdjustList: [{
    // 			adjustReason: '搞的不错',
    // 			adjustType: '0', //（0奖励、1扣分）
    // 			adjustScore: 20,
    // 		}, {
    // 			adjustReason: '不认真',
    // 			adjustType: '1', //（0奖励、1扣分）
    // 			adjustScore: 50,
    // 		}]
    // 	},]
    // }
    // ;
    constructor(props) {
        super(props);
    }

    setState = (params = {}) => {
        Object.keys(params).forEach(o => {
            this.status[o] = params[o];
        })
        return this;
    }

    @action
    getInfoCountCondition = (params = {}) => {
        let newParams = {};
        this.loadingSpin = true;
        Apis.getInfoCountCondition(newParams).then(data => {
            if (data.api_status == '1') {
                this.loadingSpin = false;
                this.isRight = data.isRight;
                if (!this.isRight) {
                    return;
                }
                this.title = data.title;
                this.responseData = { rightMenus: data.rightMenus };
                this.condition = data.infoConditionGroup;
                this.form = new WeaForm();
                this.form && !this.form.isFormInit && this.form.initFormFields(this.condition);
            } else {
                message.error(data.api_errormsg);
            }

        })
    }

    previewAndExport = (params = {}) => {
        let formParams = this.form.getFormParams();
        // if(formParams.cascader){
        //     formParams.cascader = JSON.stringify(formParams.cascader)
        // }
        let ids = '';   //选择指定单位和指定分组时得到浏览按钮中的值
        if (formParams.cascader[1] == '1' || formParams.cascader[1] == '3') {
            if (formParams.cascader[2]) {
                ids = formParams.cascader[2].value;
            } else {
                message.warn(text.NODATA);
                //this.previewFlag = flag;
                return;
            }
        }
        let newParams = {'title':formParams.title,'type':formParams.type,'dateRange':formParams.dateRange,'isShowDetail':formParams.isShowDetail,'pathid':formParams.cascader[0],'unitType':formParams.cascader[1], ids: ids, 'requestType': params.requestType };
        this.type = formParams.type;
        //判断权限
        Apis.checkInfoCountRight(newParams).then(data => {
            if (data.api_status == '1') {
                if (!data.isRight) {
                    message.error(getLabel(2012, '对不起，您暂时没有权限！'));
                    return;
                }
                this.form.validateForm().then(f => {
                    if (f.isValid) {
                        if (newParams.requestType == '0') {
                            Apis.preview(newParams).then(data => {
                                if (data.api_status == '1') {
                                    this.htmlJson = data;
                                    this.previewFlag = true;
                                } else {
                                    message.error(data.api_errormsg);
                                }
                            })
                        }
                        if (newParams.requestType == '1') {
                            //GET请求访问接口，导出word文档
                            let title = formParams.title;
                            // if(title.length %2 !== 0){
                            //     title = title+"；";
                            // }
                            let data = "action=expInfoWord&requestType=1&title=" + title + "&type=" + formParams.type + "&pathid=" + formParams.cascader[0] + "&unitType=" + formParams.cascader[1] + "&dateRange=" + formParams.dateRange + "&ids=" + ids + "&isShowDetail=" + formParams.isShowDetail;
                            let url = '/weaver/weaver.file.ExcelOut?' + data;
                            window.location = url;
                        }
        
                    } else {
                        f.showErrors();
                    }
                });
            } else {
                message.error(data.api_errormsg);
            }
        })
    }

    getRightMenu = () => {
        const { rightMenus } = this.responseData;
        let btnArr = [];
        rightMenus && rightMenus.length > 0 && rightMenus.map(m => {
            btnArr.push({
                key: m.type,
                icon: <i className={m.menuIcon} />,
                content: m.menuName,
                onClick: key => {
                    this.buttonClick(key);
                }
            })
        });
        return btnArr;
    }

    /**
   * 上方按钮
   */
    getTopButton = () => {
        const { rightMenus } = this.responseData;
        let btnArr = [];
        rightMenus && rightMenus.length > 0 && rightMenus.map((m,index) => {
            btnArr.push(
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@bi30i5@${index}`} style={{ marginLeft: "10px" }} type={"primary"}
                    onClick={() => {
                        this.buttonClick(m.type);
                    } }>
                    {m.menuName}
                </Button>
            );

        });
        return btnArr;
    }

    buttonClick(key) {
        if (key == "BTN_PREVIEW") {
            let formParams = this.form.getFormParams();
            this.setState({isShowDetail:formParams.isShowDetail});
            this.previewAndExport({ 'requestType': '0' });
        }
        if (key == 'BTN_EXPORT_WORD') {
            this.previewAndExport({ 'requestType': '1' });
        }
    }

}